package vikaa.adapter;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import vikaa.timelinepackage.AsyLoadImg;
import vikaa.timelinepackage.PinnedHeaderListView;
import vikaa.timelinepackage.PinnedHeaderListView.PinnedHeaderAdapter;
import vikaa.timelinepackage.Timeline_item;
import vikaa.tools.HttpUtil;
import vikaa.tools.StringUtils;
import vikaa.tools.ValueClass;
import vikaa.ui.TimelineForLookAround;
import vikaa.wowo.R;


import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class AdapterTimelineForLookAround extends BaseAdapter implements PinnedHeaderAdapter,//timeline gridview adapter
		OnScrollListener {
	private LayoutInflater inflater;
	private int lastItem = 0;
	private ArrayList<Timeline_item> tlitem;
	private Context context;
	private int visibleLastIndex = 0;  
    private int visibleItemCount;       
    private int count=0;
    private int first = 0;
    public static boolean ran = true;
    private long exitTime = 0;
    private long firstclick = 0;
    private long secondclick = 0;
    private boolean firsttime = true;

	public AdapterTimelineForLookAround(final LayoutInflater inflater, ArrayList<Timeline_item> tlitem,Context context) {
		this.inflater = inflater;
		this.tlitem =  tlitem;
		this.context = context;
	}

	public int getCount() {
		return tlitem.size();
	}

	public Object getItem(int position) {
		return null;
	}

	public long getItemId(int position) {
		return 0;
	}
	
	public View getView(final int position, View convertView, ViewGroup parent) {
		Holder wh;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.timeline_list_item, null);
			wh = new Holder();
			wh.avater = (ImageView) convertView.findViewById(R.id.header_avater);
	        wh.bpic = (ImageView) convertView.findViewById(R.id.bpic);
	        wh.nickname = (TextView) convertView.findViewById(R.id.header);
	        wh.created_at = (TextView) convertView.findViewById(R.id.header_create);
	        wh.comment = (TextView) convertView.findViewById(R.id.header_comment);
	        wh.text =(TextView)convertView.findViewById(R.id.example_text_view);
	        wh.commentll = (LinearLayout)convertView.findViewById(R.id.commentll);
	        wh.likenum = (TextView)convertView.findViewById(R.id.likenum);
	        wh.likeRl = (RelativeLayout)convertView.findViewById(R.id.likeRl);
	        convertView.setTag(wh);
		}else{
			wh = (Holder)convertView.getTag();
		}
		Timeline_item item = tlitem.get(position);
        wh.nickname.setText(item.getNickname());
        SimpleDateFormat sdf= new SimpleDateFormat("MM-dd HH:mm");
        Date date = new Date(Long.valueOf(item.getCreate_at())*1000);
        wh.created_at.setText(sdf.format(date));
        //text
        if(StringUtils.isEmpty(item.getText())){
        	wh.text.setVisibility(View.GONE);
        }else{
        	wh.text.setVisibility(View.VISIBLE);
        	wh.text.setText(item.getText());
        }
        //comment
        if(Integer.valueOf(item.getComments_count()) == 0){
        	wh.commentll.setBackgroundResource(R.drawable.btn_timeline_no_comment_default);
        	wh.comment.setText("");
        }else if(Integer.valueOf(item.getComments_count()) < 99){
        	wh.commentll.setBackgroundResource(R.drawable.btn_timeline_comment);
        	wh.comment.setText(item.getComments_count());
        }
        else{
        	wh.commentll.setBackgroundResource(R.drawable.btn_timeline_comment);
        	wh.comment.setText("99+");
        }
        //load avater
        if(StringUtils.isEmpty(item.getAvatar())){
        	wh.avater.setImageResource(R.drawable.wowo_item_cover);
        }else{
        	AsyLoadImg.loadAvater(HttpUtil.HttpUrlComplete(item.getAvatar()), wh.avater);
        }
        //load pic
        if(StringUtils.isEmpty(item.getThumbnail_pic())){
        	wh.bpic.setVisibility(View.GONE);
        	wh.likeRl.setVisibility(View.GONE);
        }else{
        	wh.bpic.setVisibility(View.VISIBLE);
        	wh.likeRl.setVisibility(View.VISIBLE);
        	if(TimelineForLookAround.tl.getWindowManager().getDefaultDisplay().getWidth() > ValueClass.LOW_DPI_WIDTH){
        		AsyLoadImg.loadPic(HttpUtil.HttpUrlComplete(item.getBmiddle_pic()), wh.bpic);
        	}else{
        		AsyLoadImg.loadPic(HttpUtil.HttpUrlComplete(item.getThumbnail_pic()), wh.bpic);
        	}
        }
        //likenum
        if(Integer.valueOf(item.getLike()) == 0) {
        	wh.likenum.setText("");
        }else{
        	wh.likenum.setText(item.getLike());
        }
		return convertView;
		
	}
	
	static class Holder {
    	public ImageView avater = null;
		public ImageView bpic = null;
		public TextView nickname = null;
		public TextView created_at = null;
		public TextView comment = null;
		public TextView text = null;
		public LinearLayout commentll=null;
		public TextView likenum = null;
		public RelativeLayout likeRl;
    }
	public int getPinnedHeaderState(int position) {
		/*if(position == 0){
			return PINNED_HEADER_GONE;
		}*/
		return PINNED_HEADER_PUSHED_UP;
	}

	public void configurePinnedHeader(View header, int position) {
		/*if (lastItem != position) {
			notifyDataSetChanged();
		}
		position = first;
		if(position == 0){
			getPinnedHeaderState(position);
		}
		((TextView) header.findViewById(R.id.header_text)).setText(tlitem.get(position).getNickname());
		SimpleDateFormat sdf= new SimpleDateFormat("MM-dd HH:mm");
        Date date = new Date(Long.valueOf(tlitem.get(position).getCreate_at())*1000);
		((TextView) header.findViewById(R.id.header_create)).setText(sdf.format(date));
		
		if(Integer.valueOf(tlitem.get(position).getComments_count()) == 0){
			((LinearLayout)header.findViewById(R.id.hcll)).setVisibility(View.VISIBLE);
			((LinearLayout)header.findViewById(R.id.hcll2)).setVisibility(View.INVISIBLE);
		}else if(Integer.valueOf(tlitem.get(position).getComments_count()) < 99){
			((LinearLayout)header.findViewById(R.id.hcll)).setVisibility(View.INVISIBLE);
			((LinearLayout)header.findViewById(R.id.hcll2)).setVisibility(View.VISIBLE);
			((LinearLayout)header.findViewById(R.id.hcll2)).setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
				}
			});
			((TextView) header.findViewById(R.id.header_comment)).setText(tlitem.get(position).getComments_count());
        }else{
        	((LinearLayout)header.findViewById(R.id.hcll)).setVisibility(View.INVISIBLE);
        	((LinearLayout)header.findViewById(R.id.hcll2)).setVisibility(View.VISIBLE);
        	((LinearLayout)header.findViewById(R.id.hcll2)).setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					
				}
			});
        	((TextView) header.findViewById(R.id.header_comment)).setText("99+");
        }
		
		//load avater
        if(StringUtils.isEmpty(tlitem.get(position).getAvatar())){
        	((ImageView) header.findViewById(R.id.header_avater)).setImageResource(R.drawable.wowo_item_cover);
        }else{
        	AsyLoadImg.loadCircleAvater(HttpUtil.HttpUrlComplete(tlitem.get(position).getAvatar()) , ((ImageView) header.findViewById(R.id.header_avater)));
        }
		lastItem = position+1;*/
		
	}
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		if (view instanceof PinnedHeaderListView) {
			((PinnedHeaderListView) view).configureHeaderView(firstVisibleItem);
		}
		this.first = firstVisibleItem;
		if(first != 0)first--;
		this.visibleItemCount = visibleItemCount;  
        visibleLastIndex = firstVisibleItem + visibleItemCount - 5; 
        count = totalItemCount;
	}

	public void onScrollStateChanged(AbsListView view, int scrollState) {
		int itemsLastIndex = this.getCount()-1; 
        int lastIndex = itemsLastIndex ;  
        if (scrollState == OnScrollListener.SCROLL_STATE_IDLE  
                && visibleLastIndex == lastIndex && ran /*&& (count-3) < TimelineForLookAround.count && TimelineForLookAround.count > 11*/) {  
        	
        	//Timeline.foot.setBackgroundResource(R.drawable.bg_timeline_loadmore);
        	//Timeline.listView.addFooterView(Timeline.footerview, null, false);
        	TimelineForLookAround.foot.setVisibility(View.VISIBLE);
        	TimelineForLookAround.page++;
        	TimelineForLookAround.tl.new TimelineThread(String.valueOf(TimelineForLookAround.page), TimelineForLookAround.space_id, 2).start() ;
        	ran = false;
        } /*else if ((count-3) == TimelineForLookAround.count ){
        	//Toast.makeText(context, context.getString(R.string.load_full), Toast.LENGTH_SHORT).show();
        	//Timeline.foot.setBackgroundResource(R.drawable.divider);
        	//Timeline.foot.setVisibility(View.GONE);
        	
        	if(TimelineForLookAround.tl.getWindowManager().getDefaultDisplay().getWidth() > ValueClass.LOW_DPI_WIDTH){
        		TimelineForLookAround.listView.removeFooterView(TimelineForLookAround.footerview);
        	}else{
        		TimelineForLookAround.listview2.removeFooterView(TimelineForLookAround.footerview);
        	//}
        	
        }else if((count-3) == TimelineForLookAround.count 
                && visibleLastIndex == lastIndex ){
        	Toast toast = new Toast(context);
        	toast = Toast.makeText(context, context.getString(R.string.load_full), 1500);
        	toast.setGravity(Gravity.CENTER, 0, 0);
        	toast.show();
        }*/
        
	}	
}
